package com.qingxin.wuye.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyCarportFee;

@Service
public class WyCarportFeeService extends BaseService<WyCarportFee, String> {

	
	public Pager list(String gardenid, String areaid, String carportid, String carportCode, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.entity.WyCarportFee(" +
				"c.feeid, c.carportCode, c.roomCode, c.prjcode, c.prjname, c.price, c.isfee," +
				" c.cycle, c.cycleUnit, c.cycleNum, c.beginTime, c.endTime, c.remark, g.gardenName)" +
				" from WyCarportFee c, WyGarden g" +
				" where c.gardenid=g.gardenid ");
		
		if(StringUtils.isNotEmpty(gardenid) && !"0".equals(gardenid)){
			querier.append(" and c.gardenid=:gardenid ");
			querier.setParam("gardenid", gardenid);
		}
		
		if(StringUtils.isNotEmpty(areaid) && !"0".equals(areaid)){
			querier.append(" and c.areaid=:areaid ");
			querier.setParam("areaid", areaid);
		}
		
		if(StringUtils.isNotEmpty(carportid) && !"0".equals(carportid)){
			querier.append(" and c.carportid=:carportid ");
			querier.setParam("carportid", carportid);
		}
		
		if(StringUtils.isNotEmpty(carportCode)){
			carportCode = carportCode.replace("'", "");
			querier.append(" and c.carportCode like :carportCode ");
			querier.setParam("carportCode", "%"+carportCode+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by c.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加信息
	 * @param fee
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyCarportFee fee, LoginUser loginUser, HttpServletRequest request){
		
		Date now = new Date();
		fee.setCreateTime(now);
		fee.setUpdateTime(now);
		fee.setCreateBy(loginUser.getLogin_name());
		fee.setUpdateBy(loginUser.getLogin_name());
		
		this.save(fee);
		
		return null;
	}
	
	
	/**
	 * 修改信息
	 * @param fee
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyCarportFee fee, LoginUser loginUser, HttpServletRequest request){
		WyCarportFee old = getSafeEntity(fee.getFeeid());
		if(old == null){
			return new SysError("参数错误");
		}
		
		String fields = "carportCode,roomCode,prjcode,prjname,price,isfee,cycle,cycleUnit,cycleNum,beginTime,endTime,remark";
		BeanUtil.setTargetFields(old, fee, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyCarportFee> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除信息
	 * @param feeids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] feeids, LoginUser loginUser, HttpServletRequest request){
		for(String feeid : feeids){
			WyCarportFee fee = get(feeid);
			if(fee==null){
				continue;
			}
			
			this.delete(fee);
		}
		return null;
	}
	
	
}
